package com.frxuexi.mdm2203.demo

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.frxuexi.mdm2203.R

data class InfoEntity(val img: Int, val title: String, val content: String)

@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun Demo03(/*name:String*/) {
    val list = mutableListOf<InfoEntity>()
    for (i in 1..20){
        list.add(InfoEntity(R.drawable.ic_launcher_background,"李四"+i,"我是内容描述"+i))
    }

    /*LazyColumn(verticalArrangement=Arrangement.spacedBy(10.dp), modifier = Modifier.padding(10.dp)){
        items(list){
            InfoItem(entity = it)
        }
    }*/

    /*LazyRow(horizontalArrangement=Arrangement.spacedBy(10.dp), modifier = Modifier.padding(10.dp)){
        items(list){
            InfoItem(entity = it)
        }
    }*/

    /*LazyVerticalGrid(verticalArrangement=Arrangement.spacedBy(10.dp), columns = GridCells.Fixed(2), content = {
        items(list){
            InfoItem(entity = it)
        }
    })*/

    /*LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2), content = {
        items(list){
            InfoItem(entity = it)
        }
    })*/

    val state = rememberPagerState {
        3
    }
    val imgs = listOf<String>(
        "https://img0.baidu.com/it/u=3644439246,3157535777&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1723827600&t=01f98a6b95699eca0eba9027100990c6",
        "https://img1.baidu.com/it/u=1438287640,3520327144&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1723827600&t=2ade7e7102fa343f3943531e19dfc071",
        "https://img2.baidu.com/it/u=1929064490,4234355181&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1723827600&t=ee8865bcbc61d84be9751c2a820ea384"
    )
    Column( modifier = Modifier
        .fillMaxWidth()
        .height(200.dp), horizontalAlignment = Alignment.CenterHorizontally) {
        HorizontalPager(state = state) {

                AsyncImage(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(200.dp),
                    contentScale = ContentScale.Crop,
                    model = imgs.get(it),
                    contentDescription = ""
                )
//            Text(text = it.toString())


        }
        //圆点指示器
        Row(horizontalArrangement = Arrangement.spacedBy(5.dp), modifier = Modifier.offset(y=-30.dp)) {
            repeat(3) {
                var color = if (state.currentPage == it) Color.Black else Color.Gray
                Box(
                    modifier = Modifier
                        .size(10.dp)
                        .clip(CircleShape)
                        .background(color = color)
                )
            }
        }
//        Text(text = name,modifier = Modifier.offset(y=-100.dp))


    }

//    Box(contentAlignment=Alignment.BottomEnd) {
//        Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "", modifier = Modifier.clip(
//            CircleShape))
//        Icon(imageVector = Icons.Default.CheckCircle, contentDescription = "", modifier = Modifier.offset(x=-10.dp,y=-5.dp))
//    }
}

//item布局
@Composable
fun InfoItem(entity: InfoEntity) {
    Row(Modifier.height(60.dp)) {
        Image(
            painter = painterResource(id = entity.img), contentDescription = "",
            modifier = Modifier.clip(CircleShape)
        )
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(60.dp), verticalArrangement = Arrangement.SpaceAround) {
            Text(text = entity.title)
            Text(text = entity.content)
        }
    }
}